package com.castle.fortress.admin.cms.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.cms.dto.CmsLinkCategoryDto;
import com.castle.fortress.admin.cms.entity.CmsLinkCategoryEntity;
import com.castle.fortress.admin.cms.service.CmsLinkCategoryService;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * cms友链分类管理表 api 控制器
 *
 * @author castle
 * @since 2022-02-24
 */
@Api(tags="cms友链分类管理表api管理控制器")
@RestController
@RequestMapping("/api/cms/cmsLinkCategory")
public class ApiCmsLinkCategoryController {
    @Autowired
    private CmsLinkCategoryService cmsLinkCategoryService;


    /**
     * cms友链分类管理表的分页展示
     * @param cmsLinkCategoryDto cms友链分类管理表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("cms友链分类管理表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<CmsLinkCategoryDto>> pageCmsLinkCategory(CmsLinkCategoryDto cmsLinkCategoryDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<CmsLinkCategoryDto> page = new Page(pageIndex, pageSize);

        IPage<CmsLinkCategoryDto> pages = cmsLinkCategoryService.pageCmsLinkCategory(page, cmsLinkCategoryDto);
        return RespBody.data(pages);
    }

    /**
     * cms友链分类管理表保存
     * @param cmsLinkCategoryDto cms友链分类管理表实体类
     * @return
     */
    @ApiOperation("cms友链分类管理表保存")
    @PostMapping("/save")
    public RespBody<String> saveCmsLinkCategory(@RequestBody CmsLinkCategoryDto cmsLinkCategoryDto){
        if(cmsLinkCategoryDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            CmsLinkCategoryEntity cmsLinkCategoryEntity = ConvertUtil.transformObj(cmsLinkCategoryDto,CmsLinkCategoryEntity.class);
        if(cmsLinkCategoryService.save(cmsLinkCategoryEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * cms友链分类管理表编辑
     * @param cmsLinkCategoryDto cms友链分类管理表实体类
     * @return
     */
    @ApiOperation("cms友链分类管理表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateCmsLinkCategory(@RequestBody CmsLinkCategoryDto cmsLinkCategoryDto){
        if(cmsLinkCategoryDto == null || cmsLinkCategoryDto.getId() == null || cmsLinkCategoryDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            CmsLinkCategoryEntity cmsLinkCategoryEntity = ConvertUtil.transformObj(cmsLinkCategoryDto,CmsLinkCategoryEntity.class);
        if(cmsLinkCategoryService.updateById(cmsLinkCategoryEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * cms友链分类管理表删除
     * @param ids cms友链分类管理表id集合
     * @return
     */
    @ApiOperation("cms友链分类管理表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteCmsLinkCategory(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(cmsLinkCategoryService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * cms友链分类管理表详情
     * @param id cms友链分类管理表id
     * @return
     */
    @ApiOperation("cms友链分类管理表详情")
    @GetMapping("/info")
    public RespBody<CmsLinkCategoryDto> infoCmsLinkCategory(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            CmsLinkCategoryEntity cmsLinkCategoryEntity = cmsLinkCategoryService.getById(id);
            CmsLinkCategoryDto cmsLinkCategoryDto = ConvertUtil.transformObj(cmsLinkCategoryEntity,CmsLinkCategoryDto.class);
        return RespBody.data(cmsLinkCategoryDto);
    }


}
